auto.waitFor();

//设置屏幕的宽度，像素值
var screen_width = device.width;
var widthParam = screen_width / 1440;
//设置屏幕的高度，像素值
var screen_height = device.height
var heightParam = screen_height / 3120;
// setScreenMetrics(screen_width, screen_height);
var myFunc = require("./myCommonFunction.js");


var ret = myFunc.initNetWork(null, false);
qiandao();

function qiandao() {
    try {
        myFunc.openMyApp("淘宝");
        myFunc.log("签到");
        myFunc.clickDesc("签到");
        textContains("签到").findOne(5000);
        sleep(1000);
        renwu();
        // sleep(1000);
        // click(860*widthParam, 420*heightParam);
        myFunc.log("签到完");
        myFunc.tuichu("淘宝");
    } catch (error) {
        log(error);
        myFunc.tuichu("淘宝");
        qiandao();
    }
}

function renwu() {
    myFunc.findOne('第', 3000, true);
    sleep(1000);
    click('点击领取');
    sleep(1000);
    click('立即签到');
    sleep(1000);
    click(129, 1068);
    sleep(1000);
    var a = myFunc.findOnce('去逛逛');
    while (a != null) {
        a.click();
        textContains('滑动').findOne(5000);
        sleep(1000);
        for (let index = 0; index < 11; index++) {
            scrollDown();
            sleep(3000);
        }
        back();
        sleep(2000);
        click(129, 1068);
        sleep(1000);
        a = myFunc.findOnce('去逛逛');
    }
    if (click('去搜索')) {
        var a = desc('搜索发现').findOne(3000);
        sleep(500);
        click(a.bounds().right, a.bounds().bottom + 110);
        sleep(40000);
        myFunc.tuichuToStr('连续签到领元宝');
    }
    sleep(1000);
    if (click('去浏览')) {
        sleep(40000);
        myFunc.tuichuToStr('连续签到领元宝');
    }
    sleep(1000);
    if (myFunc.findOnce('去下单') == null) {
        click(129, 1068);
        sleep(2000);
    }
    if (myFunc.findOnce('明日再来') == null && myFunc.findOnce('去逛逛') == null
        && myFunc.findOnce('去下单') != null
        && myFunc.findOnce('点击领取') == null
        && myFunc.findOnce('去搜索') == null) {
        var now = new Date();
        var date = now.getDate();
        var month = now.getMonth();
        var storage = storages.create(month + '-' + date);
        var hour = now.getHours();
        var min = now.getMinutes();
        storage.put('淘宝签到', hour + '-' + min);
    }
    click('点击领取');
}

function 淘金币() {
    clickByText("推荐");
    myFunc.log("领金币");
    clickByText("领淘金币");
    text("赚金币").findOne(5000).click();
    myFunc.log("今日任务");
    text("今日任务").findOne(5000);
    sleep(1000)
    liulan();

    text("一键领取").findOne(2000).click();
}


function liulan() {
    // 00:06:34.801/D: null 'android.view.View' 'text:浏览10s 立得' 'desc:null' 'idx:0' 'depth:18' 'top:1528' 'left:309' 'click:false'
    // 00:06:34.810/D: null 'android.widget.Button' 'text:去完成' 'desc:null' 'idx:1' 'depth:16' 'top:1449' 'left:1081' 'click:true'
    var task = className("android.view.View").indexInParent(0).depth(18).find();
    var status = className("android.widget.Button").indexInParent(1).depth(16).find();
    log(task.length, status.length)
    for (let index = 0; index < task.length; index++) {
        var a = task[index];
        var but = status[index];
        if (!/.*浏览\d*.*/.test(a.text())) {
            continue;
        }
        if (a != null && but.text().indexOf("去完成") != -1) {
            but.click();
            var xia = textContains("下滑浏览").findOne(2000);
            if (xia != null) {
                for (let index = 0; index < 10; index++) {
                    swipe(500, 500, 500, 100, 1000)
                    sleep(1000);
                }
                sleep(1000);
            } else {
                textContains("浏览得").findOne(3000);
                if (a.text().indexOf("10s") != -1) {
                    sleep(12000);
                } else {
                    sleep(17000);
                }
            }
            back();
            sleep(1000);
            liulan();
            return;
        }
    }

}



function clickByText(str) {
    var a = textContains(str).findOne(5000);
    sleep(500)
    myFunc.clickCenter(a);
}












